package com.hippo.core.ui.compose.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.GifImage
import com.hippo.core.ui.components.StateImage
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.utils.AppUtil
import com.hippo.core.vm.ScanDeviceVM

@Composable
fun ScanDeviceScreen(onBack: () -> Unit,vm: ScanDeviceVM = viewModel()) {

    val uiState = vm.uiState

    var showAddDialog by remember{ mutableStateOf(false) }
    var showConnectDialog by remember{ mutableStateOf(false) }


    if(showAddDialog){

    }

    if(showConnectDialog){
        uiState.device?.let {

        }
    }










    LaunchedEffect(uiState) {
        if(uiState.deviceList.isNotEmpty() && !showConnectDialog){
            showAddDialog = true
        }
    }


    CommonBody(
        title = stringResource(R.string.home_6),
        onBack = onBack
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()
        ) {


            Row {
                Text(
                    text = stringResource(R.string.home_7),
                    color = Color.Gray,
                    modifier = Modifier.weight(1f)
                )

                Image(
                    painter = painterResource(R.mipmap.refresh),
                    contentDescription = "Back",
                    modifier = Modifier
                        .size(18.dp)
                        .click {
                            vm.refresh()
                        }
                )

            }

            Spacer(modifier = Modifier.padding(top = 16.dp))

            Text(
                text = stringResource(R.string.home_8),
                color = Color.Black,
                fontSize = 25.sp,
            )

            Spacer(modifier = Modifier.padding(top = 16.dp))

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(
                        color = Color.White,
                        shape = RoundedCornerShape(8.dp)
                    )
                    .border(1.dp, Color(0xFFE5E5E5), shape = RoundedCornerShape(8.dp))
                    .padding(16.dp)
            ) {
                Row {

                    Text(
                        text = stringResource(R.string.home_9),
                        color = Color.Black,
                        fontSize = 16.sp,
                        modifier = Modifier.weight(1f)
                    )

                    StateImage(
                        checked = AppUtil.isBluetoothEnabled(),
                        defaultImageRes = R.mipmap.blueth_n,
                        checkedImageRes = R.mipmap.blueth_s,
                        modifier = Modifier
                            .size(18.dp)

                    )

                }

                Text(
                    text = stringResource(R.string.home_10),
                    color = Color.Gray,
                    fontSize = 12.sp,
                )

            }


            Column(
                modifier = Modifier
                    .fillMaxWidth()
            ){

                Spacer(modifier = Modifier.weight(1F))

                GifImage(
                    id = R.drawable.scan_wifi,
                    modifier = Modifier
                        .fillMaxWidth(0.8F)
                        .align(Alignment.CenterHorizontally)
                )

                Spacer(modifier = Modifier.weight(1F))

                Text(
                    text = stringResource(R.string.home_11),
                    color = MaterialTheme.colorScheme.primary,
                    textDecoration = TextDecoration.Underline,
                    fontSize = 14.sp,
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )

                Spacer(modifier = Modifier.weight(1F))


            }

        }
    }

}
//
//
//@Composable
//fun GifImage(
//    modifier:Modifier = Modifier,
//    url: Int) {
//    val context = LocalContext.current
//    var bitmap by remember { mutableStateOf<Bitmap?>(null) }
//    var animatable by remember { mutableStateOf<Animatable?>(null) }
//
//    LaunchedEffect(Unit) {
//        val request = ImageRequest.Builder(context)
//            .data(url)
//            .decoderFactory(GifDecoder.Factory())
//            .scale(Scale.FILL)
//            .build()
//
//        val result = (context as coil.ImageLoader).execute(request).drawable
//        if (result is Animatable) {
//            animatable = result
//            animatable?.start()
//        }
//        bitmap = result?.toBitmap()
//    }
//
//    DisposableEffect(animatable) {
//        onDispose {
//            animatable?.stop()
//        }
//    }
//
//    bitmap?.let {
//        Image(
//            bitmap = it.asImageBitmap(),
//            contentDescription = "GIF Animation",
//            modifier = modifier
//        )
//    }
//}


@Preview(showBackground = true)
@Composable
private fun ScanWifiScreenPreview() {
    HippoTheme {
        ScanDeviceScreen({})
    }
}